﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SAD.Common.Models;
using SAD.ClientLogic;
using SAD.ShowNodeClientLib;

namespace SAD.ShowClient.Controls
{
    /// <summary>
    /// MainControl.xaml 的交互逻辑
    /// </summary>
    public partial class MainControl : UserControl
    {
        Dictionary<string, InfoPanel> m_CategoryInfoPanels = new Dictionary<string, InfoPanel>();

        System.Threading.Thread thread = null;

        public bool Prepared { get; private set; }

        public MainControl()
        {
            InitializeComponent();

            this.Unloaded += new RoutedEventHandler(MainControl_Unloaded);
            InfoReceiverManager.Instance.AddInfoEvent += new SAD.ShowNodeClientLib.InfoReceiverManager.AddInfoHandler(Instance_AddInfoEvent);
            InfoReceiverManager.Instance.RemoveInfoEvent += new SAD.ShowNodeClientLib.InfoReceiverManager.RemoveInfoHandler(Instance_RemoveInfoEvent);

            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Start));
            thread.Start();
        }

        public void Start(object nullobj)
        {
            //
            while (true)
            {
                if (MainActivator.Current.Start())
                {
                    break;
                }
                else
                {
                    MainActivator.Current.Stop();
                }
                System.Threading.Thread.Sleep(100);
            }

            //
            this.Dispatcher.Invoke(new System.Threading.WaitCallback(delegate(object obj)
            {
                foreach (var categroy in MainActivator.Current.InfoCategorys)
                {
                    TabItem ti = new TabItem();
                    ti.Name = "ti_" + categroy.ID;
                    ti.Header = categroy.DisplayName;
                    ti.FontSize = 20;
                    ti.SetBinding(TabItem.WidthProperty, new Binding("ElementName=InfoCategoryTab, Path=Width"));
                    ti.SetBinding(TabItem.HeightProperty, new Binding("ElementName=InfoCategoryTab, Path=Height"));
                    Canvas.SetLeft(ti, 0);
                    Canvas.SetTop(ti, 0);

                    Canvas canvas = new Canvas() { Name = "canvas_" + categroy.ID };
                    canvas.SetBinding(Canvas.WidthProperty, new Binding("ElementName=" + ti.Name + ", Path=Width"));
                    canvas.SetBinding(Canvas.HeightProperty, new Binding("ElementName=" + ti.Name + ", Path=Height"));
                    ti.Content = canvas;

                    InfoPanel infoPanel = new InfoPanel(categroy.ID);
                    infoPanel.Start();

                    canvas.Children.Add(infoPanel);
                    Canvas.SetLeft(infoPanel, 0);
                    Canvas.SetTop(infoPanel, 5);
                    InfoCategoryTab.Items.Add(ti);
                    m_CategoryInfoPanels.Add(infoPanel.Name, infoPanel);

                    RightADPanel rightADPanel = new RightADPanel();
                    rightADPanel.Name = "RightADPanel_" + categroy.ID;
                    canvas.Children.Add(rightADPanel);
                    Canvas.SetLeft(rightADPanel, 1320);
                    Canvas.SetTop(rightADPanel, 0);
                }
                this.InfoCategoryTab.SelectedIndex = 0;
                this.Prepared = true;

            }), new object());
        }

        void Instance_AddInfoEvent(Info_Data info)
        {
            if (!Prepared)
            {
                return;
            }

            if (m_CategoryInfoPanels.ContainsKey("InfoPanel_0"))
            {
                InfoPanel indexInfoPanel = m_CategoryInfoPanels["InfoPanel_0"];
                this.Dispatcher.Invoke(new System.Threading.WaitCallback(delegate(object obj)
                {
                    indexInfoPanel.AddInfo(info);
                }), new object());
            }

            if (info.InfoCategoryID != 0 && m_CategoryInfoPanels.ContainsKey("InfoPanel_" + info.InfoCategoryID))
            {
                InfoPanel infoPanel = m_CategoryInfoPanels["InfoPanel_" + info.InfoCategoryID];
                this.Dispatcher.Invoke(new System.Threading.WaitCallback(delegate(object obj)
                {
                    infoPanel.AddInfo(info);
                }), new object());
            }
        }

        void Instance_RemoveInfoEvent(long infoID, int categoryID)
        {
            if (!Prepared)
            {
                return;
            }

            if (m_CategoryInfoPanels.ContainsKey("InfoPanel_0"))
            {
                InfoPanel indexInfoPanel = m_CategoryInfoPanels["InfoPanel_0"];
                this.Dispatcher.Invoke(new System.Threading.WaitCallback(delegate(object obj)
                {
                    indexInfoPanel.RemoveInfo(infoID);
                }), new object());
            }

            if (m_CategoryInfoPanels.ContainsKey("InfoPanel_" + categoryID))
            {
                InfoPanel infoPanel = m_CategoryInfoPanels["InfoPanel_" + categoryID];
                this.Dispatcher.Invoke(new System.Threading.WaitCallback(delegate(object obj)
                {
                    infoPanel.RemoveInfo(infoID);
                }), new object());
            }
        }

        void MainControl_Unloaded(object sender, RoutedEventArgs e)
        {
            InfoReceiverManager.Instance.AddInfoEvent -= new SAD.ShowNodeClientLib.InfoReceiverManager.AddInfoHandler(Instance_AddInfoEvent);
            InfoReceiverManager.Instance.RemoveInfoEvent -= new SAD.ShowNodeClientLib.InfoReceiverManager.RemoveInfoHandler(Instance_RemoveInfoEvent);
            MainActivator.Current.Stop();
            this.Prepared = false;
        }
    }
}
